﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Kugar.Core.WinForm
{
    /// <summary>
    ///     用于Winform启动的时候,自动隐藏主窗体<br/>
    ///     使用方法:<br/>
    ///            var context = new HideOnStartupApplicationContext(new mainForm());<br/>
    ///            Application.Run(context);      <br/>
    /// </summary>
    public class HideOnStartupApplicationContext : ApplicationContext
    {
        private Form mainFormInternal;// 构造函数，主窗体被存储在mainFormInternal
        public HideOnStartupApplicationContext(Form mainForm)
        {
            this.mainFormInternal = mainForm; // 当主窗体被关闭时，退出应用程序
            this.mainFormInternal.Visible = false;



            var winState = this.mainFormInternal.WindowState;
            var showInTaskbar = this.mainFormInternal.ShowInTaskbar;

            //this.mainFormInternal.WindowState = FormWindowState.Minimized;
            //this.mainFormInternal.ShowInTaskbar = false;

            this.mainFormInternal.Closed += new EventHandler(mainFormInternal_Closed);

            this.mainFormInternal.Visible = false;

            this.mainFormInternal.Show();

            this.mainFormInternal.Hide();

            ////this.mainFormInternal.WindowState = winState;
            //this.mainFormInternal.ShowInTaskbar = showInTaskbar;
            //this.mainFormInternal.WindowState = winState;

            //this.mainFormInternal.Visible = true;

        }

        void mainFormInternal_Closed(object sender, EventArgs e)
        {
            mainFormInternal.Dispose();
            Application.Exit();
        }
    }
}
